using System;
using BruteForce.NewManagers;
using BruteForce.UserInterface.Abstracts;
using BruteForce.UserInterface.UIControls;
using BruteForce.UserInterface.UIControls.MenuItems;
using BruteForce.Utilities;
using Bruteforce.UserInterface.UIControls;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace BruteForce.Screens.MenuScreen.Panels {
    public class MenuSelectionPanel : AbstractPanel {
        private int _currentUIControlIndex;

        public MenuSelectionPanel()
            : base("MenuSelectionPanel") {
            //todo - when we know which one is the longest word to change the 0 param to the length of the biggest word and the second to the biggest ...
            Rectangle screenBounds = CalculteCenteredRectangle(Bounds, 0, (int)Draws.NormalFontSize.Y * 2);
            string menuItemName = "Start Game";
            Vector2 diffBetweenLines = Draws.CalculateWordSizeNormalFont(menuItemName);
            AbstractMenuItem abstractMenuItem = new StartGameAbstractMenuItem(menuItemName,
                                                                              new Rectangle(screenBounds.X,
                                                                                            screenBounds.Y,
                                                                                            (int)diffBetweenLines.X,
                                                                                            (int)diffBetweenLines.Y));
            AddUIControl(abstractMenuItem);

            //sets the "start game" menu item as the focused one
            UIControlManager.Instance.ChangeFocousedUIControl(abstractMenuItem);

//            menuItemName = "About";
//            diffBetweenLines = Draws.CalculateWordSizeNormalFont(menuItemName);
//            abstractMenuItem = new AboutAbstractMenuItem(menuItemName,
//                                                         new Rectangle(screenBounds.X,
//                                                                       screenBounds.Y + (int)diffBetweenLines.Y,
//                                                                       (int)diffBetweenLines.X,
//                                                                       (int)diffBetweenLines.Y));
//            AddUIControl(abstractMenuItem);
        }

        public override void Update(GameTime gameTime) {
            foreach (AbstractUIControl uiControl in Children)
                uiControl.Update();
            //todo if on panel
            //if (InputManager.IsMouseClickedOn(PanelRectangle)) 

            //UIControlManager.ChangeFocousedUIControl(_uiControls[currentUIControlIndex]);
            foreach (Keys key in InputManager.CurrentClickedKeys) {
                switch (key) {
                    case Keys.Tab:
                        int focusedUIcontrolIndex = Children.IndexOf(UIControlManager.Instance.CurrentFocuseOn);
                        AbstractUIControl nextWantedFocusedUIcontrol =
                            Children[(focusedUIcontrolIndex + 1) % Children.Count];
                        UIControlManager.Instance.ChangeFocousedUIControl(nextWantedFocusedUIcontrol);
                        break;
                }
            }
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch) {

            spriteBatch.End();
            spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.Opaque, SamplerState.LinearWrap,
                              DepthStencilState.Default, RasterizerState.CullNone);
            spriteBatch.Draw(UIControlManager.Instance.BlackStoneTexture, new Vector2(Bounds.X, Bounds.Y), Bounds,
                             Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0);
            spriteBatch.End();
            spriteBatch.Begin();
            Draws.DrawBorder(Bounds, new Color(91, 91, 91), 2);


            foreach (AbstractUIControl UIcontrol in Children) {
                UIcontrol.Draw(gameTime);
            }
        }

        public override void AfterLayout()
        {
            var diffBetweenUIcomponets = 10;
            var hightDiff = UIControlManager.Instance.StartNewGameButton.Height + diffBetweenUIcomponets;

            var centerRectangle =CalculteCenteredRectangle(Bounds,
                                      UIControlManager.Instance.StartNewGameButton.Width,
                                      (hightDiff) * 5);

            var img = UIControlManager.Instance.WhiteLogoTexture;
            var logoImg = new Image(img, "Logo",
                                    new Rectangle((Bounds.Width - img.Width)/2, (centerRectangle.Y - img.Height)/2,
                                                  img.Width, img.Height));
            logoImg.SetOffset(new Vector2(Bounds.X, Bounds.Y));
            AddUIControl(logoImg);


            // StartNewGameButton
            var button = CreateButton(centerRectangle, UIControlManager.Instance.StartNewGameButton);
            button.OnClick += (() => ScreenManager.Instance.StartNewGame());


//            // ResumeGameButton
//            centerRectangle.Y += UIControlManager.Instance.ResumeButtonTexture.Height + diffBetweenUIcomponets;
//            var dynamicButton = CreateDynamicButton(centerRectangle, UIControlManager.Instance.ResumeButtonTexture, UIControlManager.Instance.ResumePressedButtonTexture, ScreenManager.Instance.CheckIfCanResume);
//            dynamicButton.OnClick += (() => ScreenManager.Instance.StartNewGame());

            //TutorialButton
            centerRectangle.Y += UIControlManager.Instance.TutorialButton.Height + diffBetweenUIcomponets;

            button = CreateButton(centerRectangle, UIControlManager.Instance.TutorialButton);
            button.OnClick += (() => ScreenManager.Instance.StartNewTutorial());

            //HelpButton
            centerRectangle.Y += UIControlManager.Instance.HelpButton.Height + diffBetweenUIcomponets;

            button = CreateButton(centerRectangle, UIControlManager.Instance.HelpButton);
            button.OnClick += (() => ScreenManager.Instance.StartInstructions());

            //CreditslButton
            centerRectangle.Y += UIControlManager.Instance.CreditslButton.Height + diffBetweenUIcomponets;

            button = CreateButton(centerRectangle, UIControlManager.Instance.CreditslButton);
            button.OnClick += (() => ScreenManager.Instance.ShowCredits());

            //ExitButton
            centerRectangle.Y += UIControlManager.Instance.RegularExitButton.Height + diffBetweenUIcomponets;

            button = CreateButton(centerRectangle, UIControlManager.Instance.RegularExitButton);
            button.OnClick += (() => BFGame.Instance.Exit());
        }


        private Button CreateButton(Rectangle centerRectangle, Texture2D image)
        {
                
                var abstractMenuItem = new Button(image, "Create Button",
                                                  new Rectangle(centerRectangle.X,
                                                                centerRectangle.Y, image.Width,
                                                                image.Height));
                abstractMenuItem.SetOffset(new Vector2(Bounds.X, Bounds.Y));
                //abstractMenuItem.OnClick += (() => ScreenManager.Instance.StartNewGame());
                AddUIControl(abstractMenuItem);
            return abstractMenuItem;
        }


        private DynamicButton CreateDynamicButton(Rectangle centerRectangle, Texture2D image1, Texture2D image2,Func<Boolean> method)
        {

            var abstractMenuItem = new DynamicButton(image1, image2, "Create Button",
                                              new Rectangle(centerRectangle.X,
                                                            centerRectangle.Y, image1.Width,
                                                            image1.Height),method );
            abstractMenuItem.SetOffset(new Vector2(Bounds.X, Bounds.Y));
            abstractMenuItem.OnClick += (() => ScreenManager.Instance.StartNewGame());
            AddUIControl(abstractMenuItem);
            return abstractMenuItem;
        }



    }
}